package com.pig4cloud.pig.admin.mq.controller;

import com.pig4cloud.pig.admin.mq.MqttConfiguration;
import com.pig4cloud.pig.admin.mq.MqttMessageSender;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttClient;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author lh
 */
@Slf4j
@RestController
@RequestMapping("mq/client")
@AllArgsConstructor
public class ClientController {

	private final MqttConnectOptions options = new MqttConnectOptions();
	@Resource
	private MqttConfiguration mqttConfiguration;

	private MqttMessageSender mqttMessageSender;



	@GetMapping("add")
	public void add(){
		try {
			String brokerUrl = "tcp://127.0.0.1:1883";
			String clientId = "abc";
			// 设置连接的用户名
			options.setUserName("jyy");
			// 设置连接的密码
			options.setPassword("jyy".toCharArray());
			options.setServerURIs(new String[]{brokerUrl});
			IMqttClient client = new MqttClient(brokerUrl, clientId);
			client.connect(options);
			client.subscribe("jyy/test");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	@GetMapping("send")
	public void send(){
		try {
			mqttMessageSender.send("abc","jyy/test/",0,"123");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
